package org.edu.modules.kwgl.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.google.common.collect.Maps;
import org.edu.modules.kwgl.entity.VeJwBkXuesheng;
import org.edu.modules.kwgl.entity.VeJwChengji;
import org.edu.modules.kwgl.mapper.VeJwChengjiMapper;
import org.edu.modules.kwgl.service.IVeJwChengjiService;
import org.edu.modules.scheduleCommon.dto.BasePageDto;
import org.edu.modules.scheduleCommon.dto.PassRateStatistics;
import org.edu.modules.scheduleCommon.dto.ScoreAnalysis;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * @Description: 成绩
 * @Author: jeecg-boot
 * @Date:   2021-05-12
 * @Version: V1.0
 */
@Service
public class VeJwChengjiServiceImpl extends ServiceImpl<VeJwChengjiMapper, VeJwChengji> implements IVeJwChengjiService {

    @Autowired
    private VeJwChengjiMapper veJwChengjiMapper;

    @Override
    public IPage<VeJwChengji> pageDto(BasePageDto page, Wrapper queryWrapper) {
        return baseMapper.pageDto(page, queryWrapper);
    }

    @Override
    public List<VeJwChengji> getByCondition(VeJwChengji condition) {
        QueryWrapper queryWrapper=new QueryWrapper<VeJwBkXuesheng>()
                //默认只筛选不及格的学生
                .eq("SFJG",0);
        if(condition.getKcid()!=null){
            queryWrapper.eq("kcId",condition.getKcid());
        }
        if(condition.getStuid()!=null){
            queryWrapper.eq("stuId",condition.getStuid());
        }
        if(condition.getXm()!=null){
            queryWrapper .eq("XM",condition.getXm());
        }
        if(condition.getGradeid()!=null){
            queryWrapper.eq("gradeId",condition.getGradeid());
        }
        if(condition.getSemid()!=null){
            queryWrapper.eq("semId",condition.getSemid());
        }
        if(condition.getSpecid()!=null){
            queryWrapper.eq("specId",condition.getSpecid());
        }

        return baseMapper.selectList(queryWrapper);
    }

    @Override
    public BigDecimal caleJgl(VeJwChengji param, Integer falid) {
        // 获取总人数
        BigDecimal totalCount = new BigDecimal(this.getCountByParam(param, falid, true));
        // 如果总人数为0，及格率也为0
        if(totalCount.equals(BigDecimal.ZERO)){
            return BigDecimal.ZERO;
        }

        // 获取及格人数
        BigDecimal jgCount = new BigDecimal(this.getCountByParam(param, falid, false));
        // 及格率，单位"%"
        BigDecimal jgl = jgCount.divide(totalCount, 4, BigDecimal.ROUND_DOWN).multiply(new BigDecimal(100));
        return jgl;
    }

    @Override
    public Map<String, JSONObject> queryAllJgl() {
        // 返回的值
        Map<String, JSONObject> data = Maps.newHashMap();
        // 查询所有专业组的所有学期的考试成绩数据
        List<JSONObject> result = getBaseMapper().selectAllCjData();
        for (JSONObject item : result){
            item.put("jgl", this.caleJgl(item.getInteger("jgs"), item.getInteger("zs")));
            data.put(item.getInteger("falid") + "_" + item.getInteger("semid"), item);
        }
        return data;
    }

    @Override
    public List<ScoreAnalysis> scoreAnalysis(Integer falId, Integer specId, Integer gradeId, Integer semId, Integer kcId) {
        return veJwChengjiMapper.scoreAnalysis(falId, specId, gradeId, semId, kcId);
    }

    @Override
    public List<PassRateStatistics> passRateStatistics(Integer falId, Integer specId, Integer gradeId, Integer semId, Integer xzbId) {
        return veJwChengjiMapper.passRateStatistics(falId, specId, gradeId, semId, xzbId);
    }

    /**
     * 计算及格率
     * @param jgs
     * @param zs
     * @return
     */
    private BigDecimal caleJgl(Integer jgs, Integer zs){
        if(zs.equals(0)){
            return new BigDecimal("0.00");
        }
        BigDecimal totalCount = new BigDecimal(zs);
        BigDecimal jgCount = new BigDecimal(jgs);

        return jgCount.divide(totalCount, 4, BigDecimal.ROUND_DOWN)
                .divide(new BigDecimal("0.01"), 2, BigDecimal.ROUND_DOWN);
    }

    /**
     * 获取人数
     * @param param 参数
     * @param falid 专业
     * @param isQueryTotal 是否查询总人数
     * @return
     */
    private int getCountByParam(VeJwChengji param, Integer falid, Boolean isQueryTotal){
        LambdaQueryWrapper<VeJwChengji> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(VeJwChengji::getSemid, param.getSemid());
        wrapper.eq(VeJwChengji::getFalid, falid);
        // 年级
        if(param.getGradeid() != null){
            wrapper.eq(VeJwChengji::getGradeid, param.getGradeid());
        }
        // 专业
        if(param.getSpecid() != null){
            wrapper.eq(VeJwChengji::getSpecid, param.getSpecid());
        }
        // 班级
        if(param.getXzbid() != null){
            wrapper.eq(VeJwChengji::getXzbid, param.getXzbid());
        }

        // 状态：及格
        if(!isQueryTotal){
            wrapper.eq(VeJwChengji::getSfjg, 1);
        }
        return this.count(wrapper);
    }
}
